#!/usr/bin/env python

import operator, platform, select, socket
import glib, gtk, gio

print "Platform:", platform.platform()
print "Python version:", platform.python_version()
print "glib/pyglib/pygio", glib.glib_version, glib.pyglib_version, gio.pygio_version

pollables = []

def add_test(obj, name, fd=None):
    if fd is None:
        fd = obj
    pollables.append((obj, name, fd))

a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
add_test(a, "unbound tcp")

a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
a.bind(('localhost', 0))
a.listen(5)
b = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
b.connect(a.getsockname())
c = a.accept()[0]
add_test(a, "listening tcp")
add_test(b, "connected tcp")
add_test(c, "accepted tcp")

a = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
a.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
add_test(a, "unbound udp broadcast")

#fd = open("whatever", "wb")
#add_fd(fd, "file for rw")

a = gio.Socket(gio.SOCKET_FAMILY_IPV4, gio.SOCKET_TYPE_DATAGRAM, gio.SOCKET_PROTOCOL_DEFAULT)
add_test(a, "udp gio.Socket", a.get_fd())

a = gio.Socket(gio.SOCKET_FAMILY_IPV4, gio.SOCKET_TYPE_STREAM, gio.SOCKET_PROTOCOL_DEFAULT)
add_test(a, "tcp gio.Socket", a.get_fd())


def print_cond(source, cond, what):
    cond_names = []
    for mask, name in cond_map.iteritems():
        if cond & mask:
            cond_names.append(name)
    print what, source, " ".join(cond_names)
    return False

cond_map = {}
all_cond = 0
for cond in "IN OUT HUP ERR NVAL PRI".split():
    mask = getattr(glib, "IO_"+cond)
    all_cond |= mask
    assert cond not in cond_map
    cond_map[mask] = cond
print "all conditions mask:", all_cond
del a

for obj, name, fd in pollables:
    try:
        print name, select.select([fd], [fd], [fd], 0)
    except select.error as exc:
        print name, exc
    glib.io_add_watch(fd, all_cond, print_cond, name)
glib.timeout_add(1000, gtk.main_quit)
gtk.main()

#Platform: Windows-7-6.1.7600
#Python version: 2.7.1
#glib/pyglib/pygio (2, 26, 0) (2, 26, 0) (2, 26, 0)
#all conditions mask: 63
#unbound tcp ([], [], [])
#listening tcp ([], [], [])
#connected tcp ([], [<socket._socketobject object at 0x025AD810>], [])
#accepted tcp ([], [<socket._socketobject object at 0x025AD848>], [])
#unbound udp broadcast ([], [<socket._socketobject object at 0x025AD880>], [])
#udp gio.Socket ([], [700], [])
#tcp gio.Socket ([], [], [])
#connected tcp <socket._socketobject object at 0x025AD810> OUT
#accepted tcp <socket._socketobject object at 0x025AD848> OUT

#Platform: Linux-2.6.32-27-generic-x86_64-with-Ubuntu-10.04-lucid
#Python version: 2.6.5
#glib/pyglib/pygio (2, 24, 1) (2, 21, 1) (2, 21, 1)
#all conditions mask: 63
#unbound tcp ([<socket._socketobject object at 0x22a8b40>], [<socket._socketobject object at 0x22a8b40>], [])
#listening tcp ([], [], [])
#connected tcp ([], [<socket._socketobject object at 0x22a8c90>], [])
#accepted tcp ([], [<socket._socketobject object at 0x22a8d00>], [])
#unbound udp broadcast ([], [<socket._socketobject object at 0x22a8d70>], [])
#udp gio.Socket ([], [11], [])
#tcp gio.Socket ([12], [12], [])
#unbound tcp <socket._socketobject object at 0x22a8b40> OUT HUP
#connected tcp <socket._socketobject object at 0x22a8c90> OUT
#accepted tcp <socket._socketobject object at 0x22a8d00> OUT
#unbound udp broadcast <socket._socketobject object at 0x22a8d70> OUT
#udp gio.Socket 11 OUT
#tcp gio.Socket 12 OUT HUP
